resource "libvirt_cloudinit_disk" "cpnode-init" {
  count = var.cp_node_count

  name = format("${var.cluster_name}-cpinit%02d.iso", count.index + 1)
  user_data = templatefile(
    "${path.module}/cloudinit/user_data.tpl",
    {
      hostname           = format("${var.cluster_name}-cp%02d", count.index + 1)
      ssh_keys           = local.all_keys
      ssh_user           = var.ssh_user
      local_admin        = var.local_admin
      local_admin_passwd = var.local_admin_passwd
      time_zone          = var.time_zone
    }
  )
  network_config = templatefile(
    "${path.module}/cloudinit/network_config.tpl",
    {
      ip_address = element(var.cp_ipv4_addresses, count.index)
      gateway    = var.network_gateway
      netmask    = var.network_netmask
      nameserver = var.nameserver
    }
  )
  pool = "boot"
}

resource "libvirt_cloudinit_disk" "worknode-init" {
  count = var.work_node_count

  name = format("${var.cluster_name}-workinit%02d.iso", count.index + 1)
  user_data = templatefile(
    "${path.module}/cloudinit/user_data.tpl",
    {
      hostname           = format("${var.cluster_name}-work%02d", count.index + 1)
      ssh_keys           = local.all_keys
      ssh_user           = var.ssh_user
      local_admin        = var.local_admin
      local_admin_passwd = var.local_admin_passwd
      time_zone          = var.time_zone
    }
  )
  network_config = templatefile(
    "${path.module}/cloudinit/network_config.tpl",
    {
      ip_address = element(var.work_ipv4_addresses, count.index)
      gateway    = var.network_gateway
      netmask    = var.network_netmask
      nameserver = var.nameserver
    }
  )
  pool = var.ci_pool
}